package cn.liontalk.vaildator;

import cn.liontalk.util.ValidateUtil;
import com.alibaba.druid.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author: 周哲
 * @package: cn.liontalk.vaildator
 * @description:
 * @date: 2019/1/19 17:23
 * @version: V1.0
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {

    private boolean required = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }


    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(required) {
            return ValidateUtil.checkPhonePattern(value);
        }else {
            if(StringUtils.isEmpty(value)) {
                return true;
            }else {
                return ValidateUtil.checkPhonePattern(value);
            }
        }
    }
}
